package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.Course;
import com.course.server.domain.CourseContent;
import com.course.server.domain.CourseExample;
import com.course.server.dto.CourseContentDto;
import com.course.server.dto.CourseDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.SortDto;
import com.course.server.mapper.CourseContentMapper;
import com.course.server.mapper.CourseMapper;

import com.course.server.mapper.My.MyCourseMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
        import java.util.Date;
/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class CourseService {
    private static final Logger Log=LoggerFactory.getLogger(CourseService.class);
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private MyCourseMapper myCourseMapper;
    @Resource
    private CourseCategoryService courseCategoryService;
    @Resource
    private CourseContentMapper courseContentMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     */
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        CourseExample courseExample = new CourseExample();
        courseExample.setOrderByClause("sort asc");
        List<Course> courseList = courseMapper.selectByExample(courseExample);
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());
        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);
        pageDto.setList(courseDtoList);
    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    @Transactional
    public void save(CourseDto courseDto){
        Course course = CopyUtil.copy(courseDto, Course.class);
        if(StringUtils.isEmpty(courseDto.getId())){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(course);
        }else {
            this.update(course);
        }
        courseCategoryService.saveBatch(course.getId(),courseDto.getCategorys());
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(Course course){
            Date now = new Date();
            course.setCreatedAt(now);
            course.setUpdatedAt(now);
        course.setId(UuidUtil.getShortUuid());
        courseMapper.insert(course);
    }
    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(Course course){
            course.setUpdatedAt(new Date());
        courseMapper.updateByPrimaryKey(course);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    public void delete(String id){
        courseMapper.deleteByPrimaryKey(id);
    }
    /**
     * create by: lin
     * description: 更新课程时长
     * create time: 2020/8/3 11:31
     */
    public void  updateTime(String courseId){
        Log.info("更新课程时长：{}",courseId);
        myCourseMapper.updateTime(courseId);
    }
    /**
     * create by: lin
     * description: 查找课程内容
     * create time: 2020/8/3 20:11
     */
    public CourseContentDto findContent(String id){
        CourseContent content = courseContentMapper.selectByPrimaryKey(id);
        if(content == null){
            return null;
        }
        return CopyUtil.copy(content,CourseContentDto.class);
    }
    /**
     * create by: lin
     * description: 保存课程内容，包含新增和修改
     * create time: 2020/8/3 20:11
     */
    public int saveContent(CourseContentDto contentDto){
        CourseContent content = CopyUtil.copy(contentDto, CourseContent.class);
        int i = courseContentMapper.updateByPrimaryKeyWithBLOBs(content);
        if(i == 0){
           i = courseContentMapper.insert(content);
        }
        return i;
    }
    /**
     * create by: lin
     * description: 排序
     * create time: 2020/8/3 11:25
     */
    @Transactional
    public void sort(SortDto sortDto){
        //修改当前记录的排序值
        myCourseMapper.updateSort(sortDto);
        //如果排序变大
        if(sortDto.getNewSort() > sortDto.getOldSort()){
            myCourseMapper.moveSortsForward(sortDto);
        }
        //如果排序变小
        if(sortDto.getNewSort() < sortDto.getOldSort()){
            myCourseMapper.moveSortsBackward(sortDto);
        }
    }
}
